/**
 * User: zhangqiang
 * Date: 15-8-11
 * Time: 上午11:04
 * 查询面板
 */
define(["require",
		"dojo/_base/declare",
		"dojo/_base/lang",
		"dojo/_base/array",
		"dojo/_base/fx",
		"dojo/fx/easing",
		"../../util/ajaxUtil",
		"dojo/dom",
		"dojo/dom-attr",
		"dojo/dom-class",
		"dojo/dom-style",
		"dojo/on",
		"dojo/query",
		"dojo/topic",
		"../BaseWidget",
		"gistech/widgets/LandNoteWidget/LandNoteWidget",
		"gistech/widgets/LandManageAndControlReportWidget/LandManageAndControlReportWidget",
		 "gistech/widgets/LandExpandManageWidget/LandExpandManageWidget",
		"gistech/widgets/SpecialDataReportWidget/SpecialDataReportWidget",
		"gistech/widgets/LandExpandSearchWidget/LandExpandSearchWidget",
		"gistech/widgets/ValueHotWidget/ValueHotWidget",
		"dijit/layout/ContentPane",
        "gistech/widgets/MapLayerResourceWidget/MapLayerResourceWidget",
        "gistech/widgets/menubarWidget/SplitScreen",
        "gistech/widgets/WYTZProjectManageWidget/WYTZProjectManageWidget",
		"dojo/text!./templates/Template.html"],
    function (require, declare, lang, array, baseFx,easing,ajaxUtil, dom, domAttr, domClass, domStyle, on, query, topic, 
    		BaseWidget,LandNoteWidget,LandManageAndControlReportWidget/*DrawGeometryWidget*/,LandExpandManageWidget,
    		SpecialDataReportWidget,LandExpandSearchWidget,ValueHotWidget,ContentPane,MapLayerResourceWidget,
    		SplitScreen,WYTZProjectManageWidget,template) {
        return declare([BaseWidget], {
            templateString: template,
            map: null,
            ismove:false,//组件位置移动判断
            righttabCon:null,
            lefttabCom:null,
            extentIndex:0,//前后视图索引
            extentFlag:false,
            lastExtent:null,//最后一个视图
            manuFlag:true,//是否手动触发
            constructor: function (args) {
                this.map = args.map;
            },
            postCreate: function () {
            	this.inherited(arguments);
            	on(this.menu1, "click",lang.hitch(this,this.SelectMenu1));
    			/*on(this.menu2, "click",lang.hitch(this,this.SelectMenu2));*/
            	on(this.landCount, "click",lang.hitch(this,this.ClusterAnalyze,'landCount'));
            	on(this.landArea, "click",lang.hitch(this,this.ClusterAnalyze,'landArea'));
            	on(this.projStat, "click",lang.hitch(this,this.ProjStat));
    			on(this.menu3, "click",lang.hitch(this,this.SelectMenu3));
    			on(this.planSchedule,"click",lang.hitch(this,this.ShowPlanScheduleDialog));
    			
    			on(this.staticReport, "click",lang.hitch(this,this.SelectMenu6));
    			on(this.themeDataStaticReport, "click",lang.hitch(this,this.SelectMenu8));//专题统计
    			/*on(this.extendControl, "click",lang.hitch(this,this.SelectMenu7));//拓展备注
    			on(this.searchExpand, "click",lang.hitch(this,this.SelectMenu10));//备注查询
    			on(this.landNote, "click",lang.hitch(this,this.SelectMenu11));//土地备注
*/    			
    			/*区域分析*/
    			on(this.regionAnaly, "mouseover",lang.hitch(this,this._showRegionAnalyPanel));
    			on(this.regionAnaly, "mouseout",lang.hitch(this,this._hideRegionAnalyPanel));
    			$('ul>li',this.regionAnaly).each(function(index,item){
    				$(item).click(function(){this._subMenuClickRegionAnaly(item);}.bind(this));
    			}.bind(this));
    			
    			/*拓展管控*/
    			/*on(this.extend, "mouseover",lang.hitch(this,this._showExtendPanel));
    			on(this.extend, "mouseout",lang.hitch(this,this._hideExtendPanel));*/
    			
    			/*聚合分析*/
    			on(this.menu2, "mouseover",lang.hitch(this,this._showChild));
    			on(this.menu2, "mouseout",lang.hitch(this,this._hideChild));
    			
    			/*工具条*/
    			ioc.map.extentNextViews=[];
    			topic.subscribe("mapExtentChanged", lang.hitch(this, this.addExtentViewToStack));//地图区域变动时重新加载
    			on(this.mapToolbar, "mouseover",lang.hitch(this,this._showChild));
    			on(this.mapToolbar, "mouseout",lang.hitch(this,this._hideChild));
    			$('ul>li',this.mapToolbar).each(function(index,item){
    				$(item).click(function(){this._subMenuClickMapToolbar(item);}.bind(this));
    			}.bind(this));
    			
    			/*工具条*/
    			on(this.splitMap, "mouseover",lang.hitch(this,this._showChild));
    			on(this.splitMap, "mouseout",lang.hitch(this,this._hideChild));
    			$('ul>li',this.splitMap).each(function(index,item){
    				$(item).click(function(){this._subMenuClickSplitMap(item);}.bind(this));
    			}.bind(this));
    			
    			/*工具条*/
    			on(this.xmTZGK, "mouseover",lang.hitch(this,this._showChild));
    			on(this.xmTZGK, "mouseout",lang.hitch(this,this._hideChild));
    			$('ul>li',this.xmTZGK).each(function(index,item){
    				$(item).click(function(){this._subMenuClickXmTZGK(item);}.bind(this));
    			}.bind(this));
    			Global.maps=[];
    			Global.mapshasaddlayers={};
    			Global.activeMapId = 'map';
            },
            startup: function () {
                this.inherited(arguments);
                this.initMenu();
            },
            
            
            //初始化显示/隐藏模块
            initMenu:function(){
            	debugger
            if(ioc.map.userRoleModule){
            	var array = $.parseJSON(ioc.map.userRoleModule)
            	var menu1 =false;
            	var menu2 =false;
            	var landCount =false;
            	var landArea =false;
            	var projStat =false;
            	var menu3 =false;
            	var regionAnaly =false;
            	var bufferRegion =false;
            	var govRegion =false;
            	var staticReport =false;
            	var planSchedule=false;
            	var themeDataStaticReport =false;
            	var xmTZGK=false;
            	var dqxm=false;
            	var wytzxm=false;
            	var zcqxm=false;
            	var zsjwsxm=false;
            	for(var i=0 ; i<array.length;i++){
	          		  var t = array[i];
	          		  if(t.MODEL_NAME =='分布分析'){menu1 = true }
	          		  else if(t.MODEL_NAME =='聚合分析'){menu2 = true }
	          		  else if(t.MODEL_NAME =='地块个数'){landCount = true }
	          		  else if(t.MODEL_NAME =='地块面积'){landArea = true }
	          		  else if(t.MODEL_NAME =='项目状态'){projStat = true }
	          		  else if(t.MODEL_NAME =='计划推进'){planSchedule = true }
	          		  else if(t.MODEL_NAME =='财富热度'){menu3 = true }
	          		  else if(t.MODEL_NAME =='区域分析'){regionAnaly = true }
	          		  else if(t.MODEL_NAME =='缓冲区'){bufferRegion = true }
	          		  else if(t.MODEL_NAME =='行政区域'){govRegion = true }
	          		  else if(t.MODEL_NAME =='管控统计'){staticReport = true }
	          		  else if(t.MODEL_NAME =='专题数据统计报表'){themeDataStaticReport = true }
	          		  else if(t.MODEL_NAME=="外业拓展"){wytzxm=true}
	          		  else if(t.MODEL_NAME=="短期项目"){dqxm=true}
	          		  else if(t.MODEL_NAME=="中长期项目"){zcqxm=true}
	          		  else if(t.MODEL_NAME=="在售及未售项目"){zsjwsxm=true}
	          		  
            	}
            	//非管理员
            	if(ioc.map.userInfoData.USERTYPE!='0'){
                	if(!menu1){this.menu1.style.display='none'}
                	if(!menu2){this.menu2.style.display='none'}
                	//控制到地块个数等
                	//if(!landCount){this.landCount.style.display='none'}
                	//if(!landArea){this.landArea.style.display='none'}
                	//if(!projStat){this.projStat.style.display='none'}
                	if(!menu3){this.menu3.style.display='none'}
                	if(!regionAnaly){this.regionAnaly.style.display='none'}
                	//if(!bufferRegion){this.bufferRegion.style.display='none'}
                	//if(!govRegion){this.govRegion.style.display='none'}
                	if(!staticReport){this.staticReport.style.display='none'}
                	if(!planSchedule){this.planSchedule.style.display='none'}
                	if(!themeDataStaticReport){this.themeDataStaticReport.style.display='none'}
                	if(!wytzxm){this.wytzxm.style.display='none'}
                	if(!dqxm){this.dqxm.style.display='none'}
                	if(!zcqxm){this.zcqxm.style.display='none'}
                	if(!zsjwsxm){this.zsjwsxm.style.display='none'}
                	if(!wytzxm&&!dqxm&&!zcqxm&&!zsjwsxm){this.xmTZGK.style.display='none'}
            	}
            	
            }
            	
            	
            },
            
            SelectMenu6:function(){
            	this.righttabCon = dijit.byId("rightTabCon");
            	console.log(this.righttabCon)
            	console.log("six");
            	this.righttabCon.removeAllTab();
            	var LandReportTab = new  ContentPane();

            	LandReportTab.set('content', "<div id='LandReportTab' ></div>");
            	this.righttabCon.addTab(LandReportTab,"",true);
            	
            	var landr = new LandManageAndControlReportWidget({map:this.map,rightTab:this.righttabCon},dojo.byId("LandReportTab"));
            	landr.startup();
            	this.setRightToolsPosition();
            	
            },
            SelectMenu8:function(){
            	this.righttabCon = dijit.byId("rightTabCon");
            	console.log(this.righttabCon);
            	this.righttabCon.removeAllTab();
            	var LandReportTab = new  ContentPane();
            	LandReportTab.set('content', "<div id='SpecialReportTab' ></div>");
            	this.righttabCon.addTab(LandReportTab,true);
            	
            	var landr = new SpecialDataReportWidget({map:this.map,rightTab:this.righttabCon},dojo.byId("SpecialReportTab"));
            	landr.startup();
            	this.setRightToolsPosition();
            },
            SelectMenu7:function(){
//            	 debugger;
            	 var landExpandManageWidget=new LandExpandManageWidget({map:this.map});
            	 landExpandManageWidget.setTitle("新增地块信息");
            	 landExpandManageWidget.startup();
            },
            SelectMenu10:function(){
            	//将左侧组件展开
    			/*if(!Global.widgets.right.hasExpand){
    				Global.widgets.right.expandLeft();
    			}*/
    			//创建高级查询Table列表
    			var landExpandSearchWidget = new LandExpandSearchWidget({"map":Global.widgets.mapWidget.map});
    			landExpandSearchWidget.startup();
            },
            SelectMenu11:function(){
//            	debugger;
            	var landNote=new LandNoteWidget({map:this.map});
            	landNote.setTitle("地块备注");
            	landNote.startup(); 
            },
            SelectMenu1:function(){
            	var chartWidget=new ChartWidget();
            	chartWidget.startup();
            },
            ShowPlanScheduleDialog:function(){
            	require(["gistech/widgets/PlanScheduleManageWidget/PlanScheduleManageWidget",], lang.hitch(this,function (PlanScheduleManageWidget){
             		if(!this.planScheduleManageWidget){
             			this.planScheduleManageWidget = new PlanScheduleManageWidget({pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:110}},dojo.byId("frontHeatDiagramDialog"));
             			this.planScheduleManageWidget.setTitle("项目计划推进管理");
             			this.planScheduleManageWidget.startup();
             		}else{
             			$(this.planScheduleManageWidget.domNode).show();
             		}
             	}));
            },
            init:function(){
            	this.lefttabCon = dijit.byId("leftTabCon");
            	
            	var ResourceTab = new  ContentPane();

            	ResourceTab.set('content', "<div id='ResourceTab' ></div>");
            	this.lefttabCon.addTab(ResourceTab,"资源列表",true);
            	
            	this.righttabCon.setWidth();
            	var mapLayerResourceWidget = new MapLayerResourceWidget({map:this.map,lefttabCon:this.lefttabCon},dojo.byId("ResourceTab"));
            	mapLayerResourceWidget.startup();
            	
            	var CityTab = new  ContentPane();

            	CityTab.set('content', "<div id='CityTab' ></div>");
            	this.lefttabCon.addTab(CityTab,"城市列表",true);
            },
            destroy: function () {
                var widgets = this.widgets;
                for (var name in widgets) {
                    if (widgets[name]) {
                        widgets[name].destroy();
                    }
                }
                this.inherited(arguments);
            },
            //显示区域分析面板
            _showRegionAnalyPanel:function(evt){
//            	debugger
            	$(this.regionAnaly).children('ul').show();
            },
            //隐藏区域分析面板
            _hideRegionAnalyPanel:function(){
            	$(this.regionAnaly).children('ul').hide();
            },
            //显示土地拓展面板
            _showExtendPanel:function(evt){
            		$(this.extend).children('ul').show();
            },
            //隐藏土地拓展面板
            _hideExtendPanel:function(){
            	$(this.extend).children('ul').hide();
            },
            
            //区域分析
            _subMenuClickRegionAnaly:function(obj){
            	switch($(obj).text()){
	            	case "缓冲区":
	            		this.basicBuffer();
	            	  break;
	            	case "行政区域":
	            		this.govRegionBuffer();
	            	  break;
            	}
            },
            //根据现有图层数据生成缓冲区
            basicBuffer:function(){
            	require(["gistech/widgets/bufferWidget/BasicBuffer",], lang.hitch(this,function (BasicBuffer){
            		Global.currentUseFun="basicBuffer";//当前使用组件的标识
             		if(!this.basicBufferDialog){
             			this.basicBufferDialog = new BasicBuffer({pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:110}},dojo.byId("frontHeatDiagramDialog"));
             			this.basicBufferDialog.setTitle("缓冲区");
             			this.basicBufferDialog.startup();
             		}else{
             			$(this.basicBufferDialog.domNode).show();
             		}
             	}));
            },
            govRegionBuffer:function(){
            	require(["gistech/widgets/GovRegionStatic/GovRegionStatic"], lang.hitch(this,function (GovRegionStatic){
//             		debugger
             		//Global.currentUseFun="govRegionStatic";//当前使用组件的标识
             		var govRegionStatic = new GovRegionStatic();
             		govRegionStatic.startup();
             		this.setRightToolsPosition();
             	}));
            },
            /**
             * 聚合分析
             */
            SelectMenu2:function(){               
            	//聚合分析查询widget的调用
            	 require(["gistech/widgets/ClusterResTree/ClusterResTree",], lang.hitch(this,function (ClusterResTree){
            		//debugger
              		var clusterResTree = new ClusterResTree({pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:90},map:this.map,clusterType:'landCount'},dojo.byId("clusterResTreeDialog"));
              		clusterResTree.setTitle("聚合分析");
              		clusterResTree.startup();
            	}));
            },
            ClusterAnalyze:function(clusterType){               
            	//聚合分析查询widget的调用
            	if(!window.clusterResTree || window.clusterResTree._destroyed){
                    require(["gistech/widgets/ClusterResTree/ClusterResTree",], lang.hitch(this,function (ClusterResTree){
                		//debugger
                    	window.clusterResTree = new ClusterResTree({pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:90},map:this.map,clusterType:clusterType},dojo.byId("clusterResTreeDialog"));
                    	window.clusterResTree.setTitle("聚合分析");
                    	window.clusterResTree.startup();
                	}));
                }
            	
            },
            _showChild:function(evt){
            	$(evt.currentTarget).children('ul').show();
            },
            _hideChild:function(evt){
            	$(evt.currentTarget).children('ul').hide();
            },
            ProjStat:function(){
            	//聚合分析查询widget的调用
            	if(!window.projTypeSelWidget || window.projTypeSelWidget._destroyed){
            		debugger
            		require(["gistech/widgets/ProjTypeSelWidget/projTypeSelWidget",], lang.hitch(this,function (ProjTypeSelWidget){
            			window.projTypeSelWidget = new ProjTypeSelWidget({pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:110},map:this.map},dojo.byId("projTypeSelDialog"));
            			window.projTypeSelWidget.setTitle("项目状态");
            			window.projTypeSelWidget.startup();
                 	}));
                }
            },
            
            SelectMenu3 : function(e) {// 财富热度
				if(this.map.getLevel()<5){
					return false;
				}
				if(this.map.getLayer("CaiFuLayer")){
					this.map.removeLayer(this.map.getLayer("CaiFuLayer"));
					var valueHot= dijit.byId("valueHot");
					valueHot.destroy();
				}else{
					var valueHot=new ValueHotWidget({map:this.map,id:"valueHot"});
//					debugger
					valueHot.startup();
				}
			},
			
			
			addExtentViewToStack:function(evt){
            	if(!ioc.map.extentLastViews){
            		ioc.map.extentLastViews=[];
            		ioc.map.extentLastViews.push(ioc.map.initExt);
            	}
            	if(this.manuFlag){
            		if(this.extentFlag){
            			ioc.map.extentLastViews.splice(0,ioc.map.extentLastViews.length);
            			this.extentFlag = false;
            			ioc.map.extentLastViews.push(this.lastExtent);
                	}
            		ioc.map.extentLastViews.push(this.map.extent);
                	this.extentIndex=ioc.map.extentLastViews.length-1;
            	}
            	this.manuFlag=true;
            },
            
			//地图工具条
            _subMenuClickMapToolbar:function(obj){
            	var extent ;
            	this.extentFlag = true;
            	if($(obj).text()=="书签"){
            		this._bookMark();
            	}else if(ioc.map.extentLastViews&&ioc.map.extentLastViews.length>1){
            		if($(obj).text()=="前一视图"){
                		if(this.extentIndex>0){
                			this.extentIndex--;
                    		extent = ioc.map.extentLastViews[this.extentIndex];
                		}
                	}else if($(obj).text()=="后一视图"){
                		if(this.extentIndex<ioc.map.extentLastViews.length-1){
                			this.extentIndex++;
                    		extent = ioc.map.extentLastViews[this.extentIndex];
                		}
                	}
                	if(extent){
                		this.manuFlag=false;
            			this.map.setExtent(new esri.geometry.Extent(extent));
            		}
            	}
            	 
            	this.lastExtent = this.map.extent;
            },
            
            //分屏
            _subMenuClickSplitMap:function(obj){
            	
            	if($(obj).text()=="一屏"){
            		SplitScreen.createMap(1,this.map);
            	}else if($(obj).text()=="二屏"){
            		SplitScreen.createMap(2,this.map);
            	}else if($(obj).text()=="三屏"){
            		SplitScreen.createMap(3,this.map);
            	}else if($(obj).text()=="四屏"){
            		SplitScreen.createMap(4,this.map);
            	}
            	
            },
            _subMenuClickXmTZGK:function(obj){
            	this.openProjectManageWidget($(obj).text(),null,null,"addstate");
            },
            openProjectManageWidget:function(ptype,feaset,flyurl,addOrEdit){
            	this.righttabCon = dijit.byId("rightTabCon");
            	console.log(this.righttabCon);
            	this.righttabCon.removeAllTab();
            	var LandReportTab = new  ContentPane();
            	LandReportTab.set('content', "<div id='XmTzgkTab' ></div>");
            	this.righttabCon.addTab(LandReportTab,true);
            	this.righttabCon.setWidgetWidth(335);
            	var landr = null;
            	
            	var projectType = "LandFeatureLayer";
            	debugger
            	if(ptype=="外业拓展"){
            		projectType = "LandFeatureLayer";
            	}else if(ptype=="短期项目"){
            		projectType = "DQXM";
            	}else if(ptype=="中长期项目"){
            		projectType = "ZCQXM";
            	}else if(ptype=="在售及未售项目"){
            		projectType = "ZSJWSXM";
            	}
            	if(dijit.byId("WYTZProjectManageWidget")){
            		dijit.byId("WYTZProjectManageWidget").destroyWidget();
            	}
            	landr = new WYTZProjectManageWidget({
            		map:this.map,
            		rightTab:this.righttabCon,
            		addOrEdit:addOrEdit,
            		feaset:feaset,
            		flyurl:flyurl,
            		projectType:projectType,
            		moduleName:ptype},dojo.byId("XmTzgkTab"));
            	landr.startup();
            	this.setRightToolsPosition();
            },
			setRightToolsPosition:function(){
				var width = $(Global.widgets.right.domNode).width();
        		$(Global.widgets.BottomArrow2.domNode).css("marginRight",width);
        		$(Global.widgets.zoombar.domNode).css("marginRight",width);
        		if(Global.widgets.BottomArrow2.toolbox){
        			$(Global.widgets.BottomArrow2.toolbox.domNode).css("marginRight",width);
        		}
			},
            
            //书签
            _bookMark:function(){
            		require(["gistech/widgets/bookMark/BookMark",], lang.hitch(this,function (BookMark){
                 			this.BookMarkDialog=new BookMark({
                 				 pos:{l:Math.ceil(document.body.clientWidth)/2-219,t:110} ,
//                 				 pos:{l:Math.ceil(document.body.clientWidth)/2-519,t:210},
//		                 				alignNode:dojo.byId("bookmark"), 
//		                            	close:true,
//		                            	hideWhenClickBody:true,
//		                                map:this.map
//                                }
//                 				 ,dojo.byId("bookMarkDialog") 
                 				alignNode:dojo.byId("bookmark"), 
                            	close:true,
                            	hideWhenClickBody:true,
                                map:this.map}
                            );
                            
                 			this.BookMarkDialog.startup();
                 		 
                 	}));
            },
            
            
        });
    });